package cn.core.sap.modules.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.core.sap.dao.mapper.Sap_tcodeMapper;
import cn.core.sap.dao.po.Sap_tcodePO;
import cn.core.sap.modules.service.Sap_tcodeService;
import cn.osworks.aos.core.asset.AOSJson;
import cn.osworks.aos.core.asset.AOSUtils;
import cn.osworks.aos.core.asset.WebCxt;
import cn.osworks.aos.core.dao.SqlDao;
import cn.osworks.aos.core.excel.POIExcelUtil;
import cn.osworks.aos.core.typewrap.Dto;
import cn.osworks.aos.core.typewrap.Dtos;
import cn.osworks.aos.core.typewrap.impl.HashDto;

/**
 * <b>事务码管理[sap_tcode] controller</b>
 * <p>
 * </p>
 * 
 * @author penghe
 * @date 2016-08-17 16:44:20
 */
@Controller
@RequestMapping(value = "core/sap_tcode")
public class Sap_tcodeController {
	@Autowired
	private Sap_tcodeService sap_tcodeservice;
	@Autowired
	private SqlDao sqlDao;
	@Autowired
	private Sap_tcodeMapper sap_tcodemapper;
	
    /**
	 * 事务码管理 页面初始化
	 * 
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "init")
	public String init(HttpSession session, HttpServletRequest request) {
		return "core/sap/sap_tcode.jsp";
	}

	/**
	 * 查询事务码管理
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "listSap_tcode")
	public void listSap_tcode(HttpServletRequest request, HttpServletResponse response) {
		Dto inDto = Dtos.newDto(request);
		HttpSession session = request.getSession();
		if (session != null)
			session.setAttribute("sap_tcodesessionid", inDto);
		//sql字定义查询 
	    //List<Sap_tcodePO> list = sqlDao.list("Sap_tcode.listSap_tcode", inDto);
		//标准查询 
		List<Sap_tcodePO> list = sap_tcodemapper.likePage(inDto);
		String outString = AOSJson.toGridJson(list, inDto.getPageTotal());
		WebCxt.write(response, outString);
	}
	/**
	 * 保存事务码管理
	 * 
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "saveSap_tcode")
	public void saveSap_tcode(HttpServletRequest request, HttpServletResponse response) {
		Dto dto = Dtos.newDto(request);
		           		 dto.put("option_date_", AOSUtils.getDateTimeStr());
		    		 dto.put("option_account_", dto.getUserInfo().getAccount_());
		    		 dto.put("option_name_", dto.getUserInfo().getName_());
		         		Dto outDto =sap_tcodeservice.saveSap_tcode(dto);
		WebCxt.write(response, AOSJson.toJson(outDto));
	}
	/**
	 * 更新事务码管理
	 * 
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updateSap_tcode")
	public void updateSap_tcode(HttpServletRequest request, HttpServletResponse response) {
		Dto dto = Dtos.newDto(request);
		           		 dto.put("option_date_", AOSUtils.getDateTimeStr());
		    		 dto.put("option_account_", dto.getUserInfo().getAccount_());
		    		 dto.put("option_name_", dto.getUserInfo().getName_());
		         		Dto outDto =sap_tcodeservice.updateSap_tcode(dto);
		WebCxt.write(response, AOSJson.toJson(outDto));
	}
	/**
	 * 删除事务码管理
	 * 
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "deleteSap_tcode")
	public void deleteSap_tcode(HttpServletRequest request, HttpServletResponse response) {
		Dto dto = Dtos.newDto(request);
		Dto outDto = sap_tcodeservice.delete(dto);
		WebCxt.write(response, AOSJson.toJson(outDto));
	}
	/**
	 * 导出事务码管理
	 * 
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "exportexcel")
	public void exportexcelSap_tcode(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
	    Dto dto = (HashDto) session.getAttribute("sap_tcodesessionid");
		String filename="";
		try {
	    filename = URLEncoder.encode("sap_tcode", "utf-8");
		InputStream bis = null;
		BufferedInputStream buffis = null;
		OutputStream fos = null;
		BufferedOutputStream bos = null;
		fos = response.getOutputStream();
		bos = new BufferedOutputStream(fos);
		String[] titleHead=new String[] { "ID", "事务码ID", "名称", "ITS_地址", "操作时间", "操作人ID", "操作人", "状态", "类型", "增加时间", };
		String[] datarow=new String[]   { "id_", "tcode_id_", "tcode_name_", "its_url_", "option_date_", "option_account_", "option_name_", "enabled_", "type_", "add_time_", };
		List<Sap_tcodePO> list = sap_tcodemapper.likePage(dto);
		List<Dto> outlist = new ArrayList<Dto>();
		for(int i=0;i<list.size();i++){
			Sap_tcodePO sap_tcodePO =  list.get(i);
			Dto dataDto = new HashDto();
			AOSUtils.copyProperties(sap_tcodePO, dataDto);
			outlist.add(dataDto);
		}
		bis = POIExcelUtil.create(filename, outlist,titleHead , datarow); 
		buffis = new BufferedInputStream(bis);
		response.setContentType("application/xls");
		response.setHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(filename + ".xls", "utf-8"));
		int bytesRead = 0;
		byte[] buffer = new byte[1024];
		while ((bytesRead = buffis.read(buffer, 0, buffer.length)) != -1) {
			bos.write(buffer, 0, bytesRead);
		}
		bos.flush();
		bis.close();
		buffis.close();
		fos.flush();
		fos.close();
		bos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
}
